<?php

$topics = app::$db->grab( 'SELECT `id`, `code`, `title`, `card` FROM `topics` WHERE `voting_public_enabled` = 1 ORDER BY `ord` ASC', array(), 'id' );

foreach( $topics as $topic_id => $topic )
{
  $topics[$topic_id]['leaders'] = app::$db->grab(     
   "SELECT r.`code`, r.`voting_title` AS 'title', r.`voting_number` as 'card_num', COUNT(v.`id`) AS 'votes'
    FROM `votes` v
    INNER JOIN `requests` r ON v.`request_id` = r.`id`
    WHERE r.`topic_id` = ?
    GROUP BY r.`topic_id`, v.`request_id`
    ORDER BY `votes` DESC",
    array( $topic_id )
    );
}

require_once HTML . 'autumn/header.php';
require_once HTML . 'autumn/cases/orgs/caves/voting.php';
require_once HTML . 'autumn/footer.php';    

exit;